package com.tedu.jsd2407_ivos_fzj.audit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.tedu.jsd2407_ivos_fzj.application.mapper.ApplicationMapper;
import com.tedu.jsd2407_ivos_fzj.application.pojo.entity.Application;
import com.tedu.jsd2407_ivos_fzj.audit.mapper.AuditMapper;
import com.tedu.jsd2407_ivos_fzj.audit.pojo.dto.AuditQuery;
import com.tedu.jsd2407_ivos_fzj.audit.pojo.dto.AuditSaveParam;
import com.tedu.jsd2407_ivos_fzj.audit.pojo.entity.Audit;
import com.tedu.jsd2407_ivos_fzj.audit.pojo.vo.AuditVO;
import com.tedu.jsd2407_ivos_fzj.audit.service.AuditService;
import com.tedu.jsd2407_ivos_fzj.base.enums.ApplicationStatusEnum;
import com.tedu.jsd2407_ivos_fzj.base.enums.AuditStatusEnum;
import com.tedu.jsd2407_ivos_fzj.user.mapper.UserMapper;
import com.tedu.jsd2407_ivos_fzj.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Service
@Slf4j
@Transactional
public class AuditServiceImpl implements AuditService {
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ApplicationMapper applicationMapper;



    @Override
    public void insertAudit(Application application) {
        log.debug("生成审批单业务，参数：{}",application);
        List<Long> auditUserIdList = application.getAuditUserIdList();
        for(int i=0;i<auditUserIdList.size();i++){
            Audit audit = new Audit();
            audit.setApplicationId(application.getId());
            audit.setAuditUserId(auditUserIdList.get(0));
            if(i==0){
                audit.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());
            }else{
                audit.setAuditStatus(AuditStatusEnum.PENDING.getCode());
            }
            audit.setAuditSort(i);
            audit.setCreateTime(new Date());
            auditMapper.insert(audit);
        }


    }

    @Override
    public List<AuditVO> selectByApplicationId(Long id) {
        log.debug("根据申请单id查询审批单业务，参数{}",id);
        List<AuditVO> auditVOList = auditMapper.selectByApplicationId(id);
        return auditVOList;

    }

    @Override
    public List<AuditVO> selectAudit(AuditQuery auditQuery) {
        log.debug("查询审批单业务，参数：{}",auditQuery);
        List<AuditVO> auditVOList = auditMapper.select(auditQuery);
        for(int i = 0;i<auditVOList.size();i++){
            AuditVO auditVO = auditVOList.get(i);
            assignAuditUserList(auditVO);
        }
        log.debug("查询审批单业务，查询结果数据：{}",auditVOList);
        return auditVOList;
    }

    @Override
    public void saveAudit(AuditSaveParam auditSaveParam) {
        log.debug("保存审批单业务，参数：{}",auditSaveParam);//id,applicationId,auditUserId,auditStatus,auditSort,rejectReason
        Audit audit = new Audit();
        BeanUtils.copyProperties(auditSaveParam,audit);
        audit.setUpdateTime(new Date());
        //审批单对应的申请单
        Application application = new Application();
        application.setId(audit.getApplicationId());
        application.setUpdateTime(new Date());
        if(audit.getAuditStatus().equals(AuditStatusEnum.AUDITED.getCode())){
            //已审核
            auditMapper.update(audit);
            AuditQuery auditQuery = new AuditQuery();
            auditQuery.setApplicationId(audit.getApplicationId());
            Integer num = auditMapper.selectRestCount(auditQuery);
            if(num>0){
                //后面还有审批人
                auditQuery.setAuditSort(audit.getAuditSort()+1);
                List<AuditVO> auditVOList = auditMapper.select(auditQuery);
                if(auditVOList!=null && !auditVOList.isEmpty()){
                    AuditVO auditVO = auditVOList.get(0);
                    Audit audit1 = new Audit();
                    audit1.setId(auditVO.getId());
                    audit1.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());
                    audit1.setUpdateTime(new Date());
                    auditMapper.update(audit1);
                }
                application.setStatus(ApplicationStatusEnum.AUDIT.getCode());
                applicationMapper.update(application);
            }else{
                //通过
                application.setStatus(ApplicationStatusEnum.AUDITED.getCode());
                applicationMapper.update(application);
            }
        }else{
            //驳回
            //1)修改当前审批人对应的审批单

            auditMapper.update(audit);
            //2)查看是否还有别的审批单数据
            AuditQuery auditQuery1 = new AuditQuery();
            auditQuery1.setApplicationId(audit.getApplicationId());
            List<AuditVO> auditVOList = auditMapper.select(auditQuery1);
            if(auditVOList!=null&&!auditVOList.isEmpty()){
                for(AuditVO auditVO :auditVOList){
                    if(auditVO.getAuditStatus().equals(AuditStatusEnum.PENDING.getCode())){
                        auditMapper.deleteById(auditVO.getId());
                    }
                }
            }
            application.setStatus(ApplicationStatusEnum.REJECT.getCode());
            application.setRejectReason(auditSaveParam.getRejectReason());
            applicationMapper.update(application);
        }
    }

    public void assignAuditUserList(AuditVO auditVO){
        List<Long> auditUserIdList = new ArrayList<>();
        List<String> auditUsernameList = new ArrayList<>();
        List<AuditVO> auditVOList = auditMapper.selectByApplicationId(auditVO.getApplicationId());
        for(int i=0;i<auditVOList.size();i++){
            Long auditUserId = auditVOList.get(i).getAuditUserId();
            auditUserIdList.add(auditUserId);
            UserVO userVO = userMapper.selectAudit(auditUserId);
            String username = userVO.getUsername();
            auditUsernameList.add(username);

        }
        StringJoiner stringJoiner = new StringJoiner(",");
        for(String username : auditUsernameList){
            stringJoiner.add(username);
        }
        auditVO.setAuditUserIdList(auditUserIdList);
        auditVO.setAuditUsernameStr(stringJoiner.toString());
    }
}
